package com.intelap.rp3.coherence.pruebaConcepto;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.intelap.rp3.coherence.pruebaConcepto.AlmacenImpl.AlmacenListener;
import com.intelap.rp3.coherence.pruebaConcepto.Cliente.Item;

public class AlmacenUI extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JButton limpiarButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AlmacenUI frame = new AlmacenUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AlmacenUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panelSur = new JPanel();
		contentPane.add(panelSur, BorderLayout.SOUTH);

		limpiarButton = new JButton("limpiar");
		panelSur.add(limpiarButton);

		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);

		table = new JTable();
		scrollPane.setViewportView(table);
	}

	private void setModelo(AlmacenUIModel modelo) {
		this.limpiarButton.setAction(modelo.limpiarAction);
		this.table.setModel(modelo.tableDataModel);
	}

	private static class AlmacenUIModel {

		public TableModel tableDataModel;
		public Action limpiarAction;

		public AlmacenUIModel(TableModel tableDataModel, Action limpiarAction) {
			this.tableDataModel = tableDataModel;
			this.limpiarAction = limpiarAction;
		}
	}

	static void showAlmacen(final AlmacenImpl almacen) {
		final Logger logger = Logger.getLogger("Almacen");

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {

					final AbstractTableModel itemsDataModel = new AbstractTableModel() {
						@Override
						public int getRowCount() {
							return almacen.getMapa().size();
						}

						@Override
						public int getColumnCount() {
							return 2;
						}

						@Override
						public Object getValueAt(int rowIndex, int columnIndex) {
							List<Map.Entry<Serializable, Serializable>> lista = new ArrayList<Map.Entry<Serializable, Serializable>>(
									almacen.getMapa().entrySet());
							Map.Entry<Serializable, Serializable> item = lista
									.get(rowIndex);

							switch (columnIndex) {
							case 0:
								return item.getKey();
							case 1:
								return item.getValue();
							default:
								throw new IndexOutOfBoundsException();
							}
						}

						@Override
						public String getColumnName(int column) {
							switch (column) {
							case 0:
								return "key";
							case 1:
								return "value";
							default:
								return null;
							}
						}
					};

					almacen.addListener(new AlmacenListener() {
						@Override
						public void mapaHaCambiado() {
							itemsDataModel.fireTableDataChanged();
						}
					});

					final Action limpiarAction = new AbstractAction("limpiar") {
						@Override
						public void actionPerformed(ActionEvent e) {
							almacen.getMapa().clear();
							itemsDataModel.fireTableDataChanged();
						}
					};

					final AlmacenUIModel almacenUiModel = new AlmacenUIModel(
							itemsDataModel, limpiarAction);

					AlmacenUI almacenUI = new AlmacenUI();
					almacenUI.setTitle("Almacen");
					almacenUI.setModelo(almacenUiModel);
					almacenUI.pack();
					almacenUI.setVisible(true);
				} catch (Exception e) {
					logger.log(Level.SEVERE, "", e);
				}
			}
		});
	}
}
